package com.project.erp.action.crm.customer;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.project.erp.action.BaseAction;
import com.project.erp.common.utils.Page;
import com.project.erp.model.crm.customer.CrmCustomer;
import com.project.erp.service.crm.customer.CrmCustomerService;

/**
 * 
 * COPYRIGHT (C) 2013 BY XXX公司

 *  功能模块名：客户关系-客户信息管理 

 *  功能描述：客户信息业务分发处理类

 * @author LiJiaGuo    

 * @version 1.0  

 * @created 2013-12-7 下午3:36:27
 */
@Controller
@Scope("prototype")
public class CrmCustomerAction extends BaseAction implements ModelDriven<CrmCustomer> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5439783989397862663L;
	
	private CrmCustomer crmCustomer;
	
	private String crmCustomerIds;
	
	@Autowired
	private CrmCustomerService crmCustomerService;

	/**
	 * 取得客户信息列表
	 * @return
	 * @throws Exception
	 */
	public String getCrmCustomerListData() throws Exception{
		logger.debug("entry getCrmCustomerListData method ...");
		try{
			Page<CrmCustomer> page = new Page<CrmCustomer>();
			page.setPageNo(getPage());
			page.setPageSize(getRows());
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
			page = crmCustomerService.getCrmCustomerPageBySearch(page, crmCustomer);
			
			//设置表格返回数据
			this.getResultMap().put("total", page.getTotalCount());
			this.getResultMap().put("rows", page.getResult());
		}catch(Exception e){
			logger.error("execute getCrmCustomerListData error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 到选择的客户信息页面
	 * @return
	 * @throws Exception
	 */
	public String toChooseListCrmcustomer() throws Exception{
		logger.debug("entry toChooseListCrmcustomer method ...");
		
		return SUCCESS;
	}
	
	/**
	 * 取得可选择的客户信息列表
	 * @return
	 * @throws Exception
	 */
	public String chooseListCrmcustomer() throws Exception{
		logger.debug("entry chooseListCrmcustomer method ...");
		try{
			Page<CrmCustomer> page = new Page<CrmCustomer>();
			page.setPageNo(getPage());
			page.setPageSize(getRows());
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
			page = crmCustomerService.getCrmCustomerPageBySearch(page, crmCustomer);
			
			//设置表格返回数据
			this.getResultMap().put("total", page.getTotalCount());
			this.getResultMap().put("rows", page.getResult());
		}catch(Exception e){
			logger.error("execute chooseListCrmcustomer error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 到添加客户信息页面
	 * @return SUCCESS
	 * @throws Exception 异常处理
	 */
	public String toAddCrmCustomer() throws Exception{
		logger.debug("entry toAddCrmCustomer method!...");
		
		return SUCCESS;
	}
	
	/**
	 * 保存客户信息信息
	 * @return SUCCESS
	 * @throws Exception 异常信息
	 */
	public String addCrmCustomer() throws Exception{
		logger.debug("entry addCrmCustomer method!...");

		try{
			crmCustomerService.saveCrmCustomer(crmCustomer);
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			String msgStr = this.getText("erp.opt.success.tip", new String[]{this.getText("erp.add"),this.getText("erp.business.crm.crmcustomer.businame")});
			this.getResultMap().put("msg",msgStr);
		}catch(Exception e){
			//返回错误信息
			logger.error("添加客户信息信息失败,失败原因:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
			String msgStr = this.getText("erp.opt.fail.tip", new String[]{this.getText("erp.add"),this.getText("erp.business.crm.crmcustomer.businame")});
			this.getResultMap().put("msg",msgStr);
		}
		return SUCCESS;
	}
	
	/**
	 * 到修改客户信息页面
	 * @return SUCCESS
	 * @throws Exception 异常处理
	 */
	public String toEditCrmCustomer() throws Exception{
		logger.debug("entry toEditCrmCustomer method!...");
		try{
			logger.info("********************crmCustomerId="+crmCustomer.getId());
			Long crmCustomerId = crmCustomer.getId();
			crmCustomer = crmCustomerService.getCrmCustomerById(crmCustomerId);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("execute toEditCrmCustomer page error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 修改客户信息信息
	 * @return
	 * @throws Exception
	 */
	public String editCrmCustomer() throws Exception{
		logger.debug("entry editCrmCustomer method!...");
		try{
			logger.info("crmCustomer="+crmCustomer);
			CrmCustomer persistentCrmCustomer = crmCustomerService.getCrmCustomerById(crmCustomer.getId());
			BeanUtils.copyProperties(crmCustomer, persistentCrmCustomer, new String[]{"createUser","createTime"});
			
			//修改客户信息信息
			crmCustomerService.editCrmCustomer(persistentCrmCustomer);
			
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			String msgStr = this.getText("erp.opt.success.tip", new String[]{this.getText("erp.edit"),this.getText("erp.business.crm.crmcustomer.businame")});
			this.getResultMap().put("msg", msgStr);
		}catch(Exception e){
			//返回错误信息
			logger.error("修改客户信息信息失败,失败原因:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
			String msgStr = this.getText("erp.opt.fail.tip", new String[]{this.getText("erp.edit"),this.getText("erp.business.crm.crmcustomer.businame")});
			this.getResultMap().put("msg", msgStr);
		}
		return SUCCESS;
	}
	
	/**
	 * 删除客户信息信息
	 * @return
	 * @throws Exception
	 */
	public String delCrmCustomer() throws Exception{
		logger.debug("entry delCrmCustomer method!...");
		try{
			logger.info("crmCustomerIds="+crmCustomerIds);
			String[] arrayIds = crmCustomerIds.split(",");
			crmCustomerService.delCrmCustomer(arrayIds);
			
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			String msgStr = this.getText("erp.opt.success.tip", new String[]{this.getText("erp.erp.delete"),this.getText("erp.business.crm.crmcustomer.businame")});
			this.getResultMap().put("msg",msgStr);
		}catch(Exception e){
			//返回错误信息
			e.printStackTrace();
			this.getResultMap().put("result",ERROR);
			String msgStr = this.getText("erp.opt.fail.tip", new String[]{this.getText("erp.erp.delete"),this.getText("erp.business.crm.crmcustomer.businame")});
			this.getResultMap().put("msg",msgStr);
		}
		return SUCCESS;
	}
	
	public String checkCrmCustomer() throws Exception{
		logger.debug("entry checkCrmCustomer method!...");
		try{
			this.getResultMap().put("checkFlag",true);
			if(StringUtils.isNotBlank(crmCustomer.getCusNameEn())){
				CrmCustomer crmCustom = crmCustomerService.getCrmCustomerByCusNameEn(crmCustomer.getCusNameEn());
				if(null != crmCustom){
					this.getResultMap().put("checkFlag",false);
				}
			}
		}catch(Exception e){
			//返回错误信息
			logger.error("entry checkCrmCustomer method exception:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
		}
		this.getResultMap().put("result",SUCCESS);
		return SUCCESS;
	}
	
	@Override
	public CrmCustomer getModel() {
		if(null == crmCustomer){
			crmCustomer = new CrmCustomer();
		}
		return crmCustomer;
	}

	public CrmCustomer getCrmCustomer() {
		return crmCustomer;
	}

	public void setCrmCustomer(CrmCustomer crmCustomer) {
		this.crmCustomer = crmCustomer;
	}

	public String getCrmCustomerIds() {
		return crmCustomerIds;
	}

	public void setCrmCustomerIds(String crmCustomerIds) {
		this.crmCustomerIds = crmCustomerIds;
	}

}
